package baidu.hudson.plugin.result.collector.MessageRouter;

import org.apache.camel.CamelContext;
import org.apache.camel.spring.SpringCamelContext;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.SystemPropertyUtils;

/**
 * User: yuxinlei
 * Date: 4/12/11
 * Time: 1:14 PM
 */
public class CamelContextLoader {


    private static final String springName = "applicationContext.xml";
    private static CamelContextLoader camelContextLoader = null;

    private static AbstractXmlApplicationContext applicationContext = null;
    private static CamelContext camelContext = null;

    private CamelContextLoader() throws Exception {
        createCamelContext();
        createApplicationContext();
    }

    public static CamelContextLoader getInstance() {
        if (camelContextLoader == null) {
            try {
                camelContextLoader = new CamelContextLoader();

            } catch (Exception e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
        return camelContextLoader;
    }

    public static CamelContext getCamelContext() {
        if (camelContext == null)
            try {
                return CamelContextLoader.getInstance().createCamelContext();
            } catch (Exception e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        return camelContext;
    }

    public static AbstractXmlApplicationContext getApplicationContext() {
        if (applicationContext == null)
            try {
                return CamelContextLoader.getInstance().createApplicationContext();
            } catch (Exception e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        return applicationContext;
    }

    /**
     * this method is used to creata a camel context , which is get from  the
     * spring context
     *
     * @return camel context
     * @throws Exception thrown when can't convet the spring appliaiton context into camel context
     */
    protected CamelContext createCamelContext() throws Exception {
        applicationContext = createApplicationContext();
        return SpringCamelContext.springCamelContext(applicationContext);
    }


    /**
     * create a spring context from the xml file location
     *
     * @return
     */
    protected AbstractXmlApplicationContext createApplicationContext() {
        //this.classLoader.getResource("/").getPath()
        String path = CamelContextLoader.class.getResource(springName).getFile() ;
        new org.springframework.context.support.FileSystemXmlApplicationContext(path) ;
        return new ClassPathXmlApplicationContext(springName);
    }


}
